#include "widget.h"
#include "ui_widget.h"
QString name;
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
//数据库连接
    db = QSqlDatabase::addDatabase("QMYSQL","connection");
    db.setHostName("127.0.0.1");
    db.setPort(3306);
    db.setDatabaseName("experimentdb");
    db.setUserName("root");
    db.setPassword("1234");
    if(db.open()){
        qDebug("SQL connect success!");
    }else{
        qDebug("SQL connect failed!");
    }
}

Widget::~Widget()
{
    delete ui;
}

//登录时权限管理
void Widget::on_Sign_in_clicked()
{

    useraccount = ui->UserAccount ->text();
    password = ui->Password ->text();
//管理员登录
    if(useraccount =="admin"&&password =="1112"&&ui->bootuser->isChecked())
    {
        qDebug("welcome bootuser");
        QMessageBox::information(this,"登录提示","登录成功！");
        bootuser *bootUser =new bootuser;
        bootUser->show();
        ui->UserAccount->clear();
        ui->Password->clear();
    }
    else if (ui->teacher->isChecked())//教师登录
    {
        identity = "教师";
        cmd = QString("select * from account where account_identity ='%1' and account_id ='%2'").arg(identity).arg(ui->UserAccount->text());
        QSqlQuery query(cmd,db);
        while(query.next()){
            db_name = query.value(0).toString();
            psd = query.value(2).toString();
        }
        if(psd == password){
            qDebug("welcome teacher");
            name = db_name;
            QMessageBox::information(this,"登录提示","登录成功！");
            tea_user *teaUser =new tea_user;
            teaUser->show();
            ui->UserAccount->clear();
            ui->Password->clear();
        }else{
            QMessageBox::critical(this,"登录提示","登录失败！查无此账号！");
        }
    }
    else if(ui->student->isChecked())//学生登录
    {
        identity = "学生";
        cmd = QString("select * from account where account_identity ='%1' and account_id='%2'").arg(identity).arg(ui->UserAccount->text());
        QSqlQuery query(cmd,db);
        while(query.next()){
            db_name =query.value(0).toString();
            psd =query.value(2).toString();
        }
        if(psd == password){
        qDebug("welcome student");
        name =db_name;
        QMessageBox::information(this,"登录提示","登录成功！");
        stu_user *stuUser =new stu_user;
        stuUser->show();
        ui->UserAccount->clear();
        ui->Password->clear();
        }else{
            QMessageBox::critical(this,"登录提示","登录失败！查无此账号！");
        }
    }
    else{ QMessageBox::critical(this,"登录提示","登录失败！");}
}

//进入注册界面
void Widget::on_Sign_up_clicked()
{
    login *login_ui =new login;
    login_ui->show();
}
